package com.icss.flow.ddl;

import com.icss.flow.pojo.entity.TMetaEntity;
import com.icss.flow.pojo.entity.TMetaField;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

@Service
public class MySQLDDL implements DBDDL {

    private static final String i = "ALTER TABLE `%s` DROP COLUMN `%s`";
    private static final String J = "ALTER TABLE `%s` ADD COLUMN `%s` %s NULL DEFAULT NULL";
    private static final String h = "  CREATE TABLE `%s_share` (`autoId` INT(11) NOT NULL AUTO_INCREMENT,`recordId` CHAR(40) NOT NULL,`principalId` CHAR(40) NOT NULL,`readable` TINYINT(4) NULL DEFAULT '0',`updatable` TINYINT(4) NULL DEFAULT '0',`deletable` TINYINT(4) NULL DEFAULT '0',`assignable` TINYINT(4) NULL DEFAULT '0',`shareable` TINYINT(4) NULL DEFAULT '0',PRIMARY KEY (`autoId`) USING BTREE,UNIQUE INDEX `recordId_principalId` (`recordId`, `principalId`) USING BTREE,INDEX `recordId_principalId_readable` (`recordId`, `principalId`, `readable`) USING BTREE)COLLATE='utf8mb4_general_ci'ENGINE=InnoDB;";
    private static final String createEntityTemplate = "CREATE TABLE `%s`(autoId INT(11) NOT NULL AUTO_INCREMENT,%s CHAR(40) NOT NULL,`createdOn` DATETIME NOT NULL,`createdBy` CHAR(40) NOT NULL,`ownerUser` CHAR(40) NOT NULL,`ownerDepartment` CHAR(40) NOT NULL,`modifiedOn` DATETIME NULL DEFAULT NULL,`modifiedBy` CHAR(40) NULL DEFAULT NULL,`isDeleted` TINYINT(4) NULL DEFAULT '0',PRIMARY KEY(`autoId`) USING BTREE,UNIQUE INDEX `%s` (`%s`) USING BTREE,INDEX `ownerUser` (`ownerUser`) USING BTREE,INDEX `ownerDepartment` (`ownerDepartment`) USING BTREE,INDEX `isDeleted` (`isDeleted`) USING BTREE )COLLATE='utf8mb4_general_ci'ENGINE=InnoDB;";
    private static final String H = "CREATE TABLE `%s`(autoId INT(11) NOT NULL AUTO_INCREMENT,%s CHAR(40) NOT NULL,`createdOn` DATETIME NOT NULL,`createdBy` CHAR(40) NOT NULL,`modifiedOn` DATETIME NULL DEFAULT NULL,`modifiedBy` CHAR(40) NULL DEFAULT NULL,`isDeleted` TINYINT(4) NULL DEFAULT '0',PRIMARY KEY(`autoId`) USING BTREE,UNIQUE INDEX `%s` (`%s`) USING BTREE,INDEX `isDeleted` (`isDeleted`) USING BTREE )COLLATE='utf8mb4_general_ci'ENGINE=InnoDB;";
    private final JdbcTemplate jdbcTemplate;
    private static final String e = "DROP TABLE IF EXISTS `%s_share`";
    private static final String F = "ALTER TABLE `%s` MODIFY COLUMN `%s` %s NULL DEFAULT NULL";
    private static final String ALLATORIxDEMO = "DROP TABLE `%s`";

    public MySQLDDL(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }


    @Override
    public void alterFieldColumn(TMetaField tMetaField) {

    }

    @Override
    public void deleteEntityTable(TMetaEntity tMetaEntity) {

    }

    @Override
    public void createEntityTable(TMetaEntity tMetaEntity) {
//        String IdName = tMetaEntity.getIdField().getPhysicalName();
//        Object[] param = new String[4];
//        param[0] = tMetaEntity.getPhysicalName();
//        param[1] = IdName;
//        param[2] = IdName;
//        param[3] = IdName;
//        String s = String.format(createEntityTemplate, param);
//        jdbcTemplate.execute(s);
    }

    @Override
    public void createFieldColumn(TMetaField tMetaField) {

    }

    @Override
    public void deleteFieldColumn(TMetaField tMetaField) {

    }
}
